/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

/**
 *
 * @author VUHOANG
 */
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class MySQLDataAccessHelper {
    
    private Connection connection;

    public void open() {
        try {
            Driver d = new org.gjt.mm.mysql.Driver();
            DriverManager.registerDriver(d);
            String url = "jdbc:mysql://localhost:3306/qlbh";
            Properties info = new Properties();
            info.setProperty("characterEncoding", "utf-8");
            info.setProperty("user", "root");
            info.setProperty("password", "root");
            this.connection = DriverManager.getConnection(url, info);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    public void close() {
        try {
            this.connection.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    public ResultSet executeQuery(String sql) {
        ResultSet rs = null;
        try {
            Statement sm = this.connection.createStatement();
            rs = sm.executeQuery(sql);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return rs;
    }

    public int executeUpdate(String sql) {
        int n = -1;
        try {
            Statement sm = this.connection.createStatement();
            n = sm.executeUpdate(sql);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return n;
    }
}

